package com.hpq.chat.dao;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hpq.chat.domain.model.Contact;
import com.hpq.chat.domain.model.Message;
import com.hpq.chat.domain.vo.request.ChatMessageReadReq;
import com.hpq.chat.mapper.ContactMapper;
import com.hpq.chat.service.ContactService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hpq.common.domain.vo.request.CursorPageBaseReq;
import com.hpq.common.domain.vo.response.CursorPageBaseResp;
import com.hpq.common.utils.CursorUtils;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 会话列表 服务实现类
 * </p>
 *
 * @author hpq
 * @since 2024-12-16
 */
@Service
public class ContactDAO extends ServiceImpl<ContactMapper, Contact>  {
    /**
     * 更新所有人的会话时间，没有就直接插入
     */
    public void refreshOrCreateActiveTime(Long roomId, List<Long> memberUidList, Long msgId, Date activeTime) {
        baseMapper.refreshOrCreateActiveTime(roomId, memberUidList, msgId, activeTime);
    }

    public Contact getContact(Long receiveUid, Long roomId) {
        return lambdaQuery()
                .eq(Contact::getRoomId, roomId)
                .eq(Contact::getUid, receiveUid)
                .one();
    }

    public CursorPageBaseResp<Contact> getContactPage(Long uid, CursorPageBaseReq req) {
        return CursorUtils.getCursorPageByMysql(this, req,wrapper ->{
            wrapper.eq(Contact::getUid, uid);
        }, Contact::getActiveTime);
    }
    public List<Contact> getByRoomIds(List<Long> roomIds, Long uid) {
        return lambdaQuery()
                .in(Contact::getRoomId, roomIds)
                .eq(Contact::getUid, uid)
                .list();
    }

    public CursorPageBaseResp<Contact> getReadPage(Message message, ChatMessageReadReq req) {
        return CursorUtils.getCursorPageByMysql(this,req,wrapper ->{
            wrapper.eq(Contact::getRoomId, message.getRoomId())
                    .ne(Contact::getUid, message.getFromUid())
                    .ge(Contact::getReadTime, message.getCreateTime());
        },Contact::getReadTime);
    }
    public CursorPageBaseResp<Contact> getUnReadPage(Message message, ChatMessageReadReq req){
         return CursorUtils.getCursorPageByMysql(this,req,wrapper ->{
            wrapper.eq(Contact::getRoomId, message.getRoomId())
                    .ne(Contact::getUid, message.getFromUid())
                    .lt(Contact::getReadTime, message.getCreateTime());
        },Contact::getReadTime);
    }
    public Integer getTotalCount(Long roomId) {
        return lambdaQuery()
                .eq(Contact::getRoomId, roomId)
                .count();
    }
    public Integer getUnReadCount(Message message) {
        return lambdaQuery()
                .eq(Contact::getRoomId, message.getRoomId())
                .lt(Contact::getReadTime, message.getCreateTime())
                .count();
    }

    public Integer getReadCount(Message message) {
        return lambdaQuery()
                .eq(Contact::getRoomId, message.getRoomId())
                .ne(Contact::getUid, message.getFromUid())
                .ge(Contact::getReadTime, message.getCreateTime())
                .count();
    }

    public Contact get(Long uid, Long roomId) {
        return lambdaQuery()
                .eq(Contact::getUid, uid)
                .eq(Contact::getRoomId, roomId)
                .one();
    }

    public Boolean removeByRoomId(Long roomId, List<Long> uidList) {
        if (CollectionUtil.isNotEmpty(uidList)) {
            LambdaQueryWrapper<Contact> wrapper = new QueryWrapper<Contact>().lambda()
                    .eq(Contact::getRoomId, roomId)
                    .in(Contact::getUid, uidList);
            return this.remove(wrapper);
        }
        return false;
    }
}
